package structs;

/**
 * Represents a 3d vector in cartesian coordinates.
 * 
 * @author Simon
 * 
 */
public class Vector3 {
	public float X;
	public float Y;
	public float Z;

	public Vector3(float x, float y, float z) {
		X = x;
		Y = y;
		Z = z;
	}

	public Vector3 multiply(float a) {
		return new Vector3(a * X, a * Y, a * Z);
	}
	
	public float dotproduct(Vector3 v)
	{
		return X*v.X + Y*v.Y + Z*v.Z;
	}

	public Vector3 plus(Vector3 a) {
		return new Vector3(X + a.X, Y + a.Y, Z + a.Z);
	}
	
	public void transform(Matrix m)
	{
		float[][] data = m.getData();
		float oriX = X;
		float oriY = Y;
		float oriZ = Z;
		
		X = data[0][0]*oriX + data[0][1]*oriY + data[0][2]*oriZ + data[0][3];
		Y = data[1][0]*oriX + data[1][1]*oriY + data[1][2]*oriZ + data[1][3];
		Z = data[2][0]*oriX + data[2][1]*oriY + data[2][2]*oriZ + data[2][3];
		
		discretize();
	}
	
	private void discretize()
	{
		if(Math.abs(X)<0.01f)
			X = 0;
		if(Math.abs(Y)<0.01f)
			Y = 0;
		if(Math.abs(Z)<0.01f)
			Z = 0;
	}
	
	public float length()
	{
		return (float)Math.sqrt(X*X+Y*Y+Z*Z);
	}
	
	public void normalize()
	{
		float length = length();
		X = X/length;
		Y = Y/length;
		Z = Z/length;
	}
	
}
